﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EEPNetFLClient
{
    public partial class frmWaiting : Form
    {
        static frmWaiting _from;
        static bool _showInTaskBar;
        static string _msg;

        public frmWaiting()
        {
            InitializeComponent();
            this.TopMost = true;

            string picPath = Application.StartupPath + "\\Config\\wait.gif";
            if (System.IO.File.Exists(picPath))
            {
                this.pictureBox1.Image = Image.FromFile(picPath);
            }
        }

        /// <summary>
        /// 显示正在运行窗体.
        /// </summary>
        /// <param name="showInTaskbar">正在运行窗体是否在任务栏显示</param>
        public static void ShowWaiting(bool showInTaskBar,string msg)
        {
            _showInTaskBar = showInTaskBar;
            _msg = msg;
            System.Threading.Thread _th = new System.Threading.Thread(new System.Threading.ThreadStart(OpenForm));
            _th.Start();
        }

        /// <summary>
        /// 
        /// </summary>
        private static void OpenForm()
        {
            Cursor.Current = Cursors.WaitCursor;
            if (_from == null)
            {
                _from = new frmWaiting();
            }
            _from.ShowInTaskbar = _showInTaskBar;
            _from.SetMessage(_msg);
            if (_from.Visible)
                return;
            _from.ShowDialog();
        }

        delegate void delegateCloseSelf();

        /// <summary>
        /// 关才正在运行窗体
        /// </summary>
        public static void CloseWaiting()
        {
            if (_from == null)
                return;
            if (_from.InvokeRequired)
            {
                delegateCloseSelf _dc = new delegateCloseSelf(_from.Kill);
                _from.Invoke(_dc);
            }
            else
            {
                _from.Kill();
            }
        }

        void Kill()
        {
            this.Hide();
            Cursor.Current = Cursors.Default;
        }

        delegate void delegateSetMessage(string msg);
        private void SetMessage(string msg)
        {
            this.label1.Text = msg;
            Application.DoEvents();
        }

        public static void UpdateMessage(string msg)
        {
            if (_from == null)
                return;
            if (_from.InvokeRequired)
            {
                _from.Invoke(new delegateSetMessage(_from.SetMessage), new object[] { msg });
            }
            else
            {
                _from.SetMessage(msg);
            }
        }
    }
}

